package com.feijia.aijingjingus;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.mob.MobSDK;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

import static com.feijia.aijingjingus.GlobalHelper.NickName;

public class TestPI extends AppCompatActivity {

    LoadingDialog ld;
    TextView etNickName;
    TextView tvUserNum;
    TextView etFirstDay, etLastDay;
    TextView etMenstruation;
    TextView etAveragePeriodLength;
    TextView etAge;
    OptionsPickerView pvNickName, pvAge, pvMenstruation, pvPeriod, pvLogout, pvFeedback, pvAssociatedCode;
    TimePickerView pvFirstDay;

    private ImageView mImageViewImage;
    int Age;
    private RelativeLayout rlMain, rlLoading;
    Handler handler = new Handler();
    Context context = this;
    private SharedPreferences sp;
    private static final int PICTURE_FROM_CAMERA = 101;
    private static final int PICTURE_FROM_GALLERY = 102;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private boolean gettingAssociatedCode = false;

    private File file;

    Runnable UpdateOp;

    private EventHandler eventHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_information);

        etNickName = (TextView) findViewById(R.id.etNickName);
        etFirstDay = (TextView) findViewById(R.id.etFirstDay);
        etLastDay = (TextView) findViewById(R.id.etLastDay);
        etMenstruation = (TextView) findViewById(R.id.etMenstruation);
        etAveragePeriodLength = (TextView) findViewById(R.id.etAveragePeriodLength);
        etAge = (TextView) findViewById(R.id.etAge);
        rlMain = (RelativeLayout) findViewById(R.id.rlUser);
        rlLoading = (RelativeLayout) findViewById(R.id.rlUserLoading);
        tvUserNum = (TextView) findViewById(R.id.tvUserNum);

        //初始化SDK
        MobSDK.init(this,"26e60d71d9250","fa62a681a9d3fb8d609a91f60eb554f5");

        // 创建EventHandler对象
        eventHandler = new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (data instanceof Throwable) {
                    Throwable throwable = (Throwable)data;
                    final String msg = throwable.getMessage();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(TestPI.this, msg, Toast.LENGTH_SHORT).show();
                        }
                    });
                } else {
                    if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        // 这里是验证成功的回调，可以处理验证成功后您自己的逻辑，需要注意的是这里不是主线程
                        Intent intent = new Intent();
                        intent.setClass(getApplicationContext(),controlCenterActivity.class);
                        startActivity(intent);
                    }
                }
            }
        };

        // 注册监听器
        SMSSDK.registerEventHandler(eventHandler);

        /*// 打开注册页面
        RegisterPage registerPage = new RegisterPage();

        registerPage.setRegisterCallback(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                // 解析注册结果
                if (result == SMSSDK.RESULT_COMPLETE) {
                    @SuppressWarnings("unchecked")
                    HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
                    String country = (String) phoneMap.get("country");
                    String phone = (String) phoneMap.get("phone");
                    // 提交用户信息
                    //registerUser(country, phone);
                }
            }
        });
        registerPage.show(this);*/
    }

    protected void onDestroy(){
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eventHandler);
    }

    public void onClickNickName(View view) {
        pvNickName = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                //String tx = cardItem.get(options1).getPickerViewText();
                //btn_CustomOptions.setText(tx);
            }
        })
                .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        final EditText etDialogNickName = (EditText) v.findViewById(R.id.etDialogNickName);
                        etDialogNickName.setText(NickName);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                etNickName.setText(etDialogNickName.getText());
                                new Thread(UpdateOp).start();
                                pvNickName.returnData();
                                pvNickName.dismiss();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvNickName.dismiss();
                            }
                        });

                    }
                })
                .isDialog(true)
                .build();
        pvNickName.show();
    }

    public void onClickPickAge(View view) {
        final int StartAge = 14;
        pvAge = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                etAge.setText("" + (StartAge + options1));
                new Thread(UpdateOp).start();
            }
        })
                .setLayoutRes(R.layout.pickerview_custom_age_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvAge.returnData();
                                pvAge.dismiss();
                            }
                        });

                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvAge.dismiss();
                            }
                        });
                    }
                })
                .setContentTextSize(36)
                .isDialog(true)
                .setBgColor(0xEEEEEE)
                .build();
        pvAge.setPicker(new ArrayList<String>() {
            {
                for (int i = StartAge; i < 81; i++) {
                    add("" + i);
                }
            }
        });
        pvAge.setSelectOptions(10);
        pvAge.show();
    }

    public void onClickMenstruation(View view) {
        final int StartMenstruation = 20;
        pvMenstruation = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {

                etMenstruation.setText("" + (StartMenstruation + options1));
                new Thread(UpdateOp).start();
            }
        })
                .setLayoutRes(R.layout.pickerview_custom_menstruation_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvMenstruation.returnData();
                                pvMenstruation.dismiss();
                            }
                        });

                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvMenstruation.dismiss();
                            }
                        });
                    }
                })
                .isDialog(true)
                .setContentTextSize(36)
                .setBgColor(0xEEEEEE)
                .build();
        pvMenstruation.setPicker(new ArrayList<String>() {
            {
                for (int i = StartMenstruation; i < 41; i++) {
                    add("" + i);
                }
            }
        });
        pvMenstruation.setSelectOptions(8);
        pvMenstruation.show();
    }

    public void onClickAveragePeriodLength(View view) {
        final int StartPeriod = 2;
        pvPeriod = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {

                etAveragePeriodLength.setText("" + (StartPeriod + options1));
                new Thread(UpdateOp).start();
            }
        })
                .setLayoutRes(R.layout.pickerview_custom_period_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvPeriod.returnData();
                                pvPeriod.dismiss();
                            }
                        });

                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvPeriod.dismiss();
                            }
                        });
                    }
                })
                .isDialog(true)
                .setContentTextSize(36)
                .setBgColor(0xEEEEEE)
                .build();
        pvPeriod.setPicker(new ArrayList<String>() {
            {
                for (int i = StartPeriod; i < 8; i++) {
                    add("" + i);
                }
            }
        });
        pvPeriod.setSelectOptions(2);
        pvPeriod.show();
    }

    public void onClickFirstDay(View view) {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        //startDate.set(2013,1,1);
        Calendar endDate = Calendar.getInstance();
        //endDate.set(2020,1,1);

        //正确设置方式 原因：注意事项有说明
        startDate.set(2013, 0, 1);
        endDate.set(2130, 11, 31);
        pvFirstDay = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                etFirstDay.setText(format.format(date));
                new Thread(UpdateOp).start();
                //tvTime.setText(getTime(date));
            }
        }).setLayoutRes(R.layout.pickerview_custom_firstday_options, new CustomListener() {
            @Override
            public void customLayout(View v) {
                //自定义布局中的控件初始化及事件处理
                final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pvFirstDay.returnData();
                        pvFirstDay.dismiss();
                    }
                });
                ivCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pvFirstDay.dismiss();
                    }
                });

            }
        })
                .setLabel("", "", "", "", "", "")
                .setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                .isCyclic(true)//是否循环滚动
                .setContentSize(24)
                .setBgColor(0xEEEEEE)//滚轮背景颜色 Night mode
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(true)//是否显示为对话框样式
                .build();
        pvFirstDay.show();
    }

    public void PostMsg(View view) {
        Intent intent = new Intent();
        intent.setClass(this,controlCenterActivity.class);
        startActivity(intent);
    }
}
